package com.tianji.remark.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.exceptions.DbException;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.domain.dto.LikeRecordFormDTO;
import com.tianji.remark.domain.dto.LikedTimesDTO;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.tianji.remark.service.ILikedStatService;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-12-27
 */
//@Service
@RequiredArgsConstructor
public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {

    private final ILikedStatService statService;
    private final RabbitMqHelper mqHelper;

    /**
     * 点赞或者取消点赞
     * @param recordDTO
     */
    @Override
    public void addLikeRecord(LikeRecordFormDTO recordDTO) {
        Long userId = UserContext.getUser();
        //判断是点赞还是取消点赞
        if (recordDTO.getLiked()){
            //点赞，看是否已经点赞过了
            Integer count = this.lambdaQuery().eq(LikedRecord::getBizId, recordDTO.getBizId())
                    .eq(LikedRecord::getUserId, userId)
                    .count();
            if (count> 0){
                return;
            }
            //没有点赞过，直接新增
            LikedRecord likedRecord = BeanUtils.copyBean(recordDTO, LikedRecord.class);
            likedRecord.setUserId(UserContext.getUser());
            boolean result = save(likedRecord);
            if (!result){
                throw new DbException("新增记录失败");
            }
        }else{
            //取消点赞。直接删除记录就好
            LambdaUpdateWrapper<LikedRecord> lqw = new LambdaUpdateWrapper<>();
            lqw.eq(LikedRecord::getBizId, recordDTO.getBizId());
            lqw.eq(LikedRecord::getUserId, userId);
            boolean result = remove(lqw);
            if (!result){
               return;
            }
        }
        //统计点赞数量，发送消息
        Integer count = this.lambdaQuery()
                .eq(LikedRecord::getBizId, recordDTO.getBizId())
                .count();
        //发送消息
        String exchange = MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
        String key = StringUtils.format(MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE, recordDTO.getBizType());
        LikedTimesDTO likedTimesDTO = new LikedTimesDTO();
        likedTimesDTO.setBizId(recordDTO.getBizId());
        likedTimesDTO.setLikedTimes(count);
        mqHelper.send(exchange,key,likedTimesDTO);
    }


    /**
     * 批量查询点赞状态
     * @param bizIds
     * @return
     */
    @Override
    public Set<Long> isBizLiked(List<Long> bizIds) {
        //查询当前用户的对回答或评论，或者文章
        Long userId = UserContext.getUser();
        List<LikedRecord> recordList = this.lambdaQuery()
                .eq(LikedRecord::getUserId, userId)
                .in(LikedRecord::getBizId, bizIds)
                .list();
        return recordList.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
    }


}

